package com.boyuan.huijiaoyu;

import com.example.contactfragment.ContactGroupService;
import com.magicsoft.app.wcf.CaseService;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class ChangeGroupnameActivity extends Activity implements OnClickListener{

	private RelativeLayout lay_quxiao;
	private EditText edit_groupname;
	private String groupName;
	private String groupId;
	private ContactGroupService contactgroupService;
	private CaseService caseService;
	private String caseid;
	private String yonghuID;
	private String realname;
	private String groupname1;
	private RelativeLayout lay_ok;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_groupname);
		initUI();
	}

	private void initUI() {
		Intent intent = getIntent();
		groupName = intent.getStringExtra("groupname");
		groupId = intent.getStringExtra("groupId");
		caseid = intent.getStringExtra("caseid");
		yonghuID = intent.getStringExtra("yonghuID");
		realname = intent.getStringExtra("realname");
		lay_ok = (RelativeLayout) findViewById(R.id.lay_ok);
		lay_ok.setOnClickListener(this);
		lay_quxiao = (RelativeLayout) findViewById(R.id.lay_back_groupname);
		lay_quxiao.setOnClickListener(this);
		edit_groupname = (EditText) findViewById(R.id.ed_groupname);
		edit_groupname.setText(groupName);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.change_groupname, menu);
		return true;
	}
	
//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		finish();
//		return super.onTouchEvent(event);
//	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_ok:
			groupname1 = edit_groupname.getText().toString().trim();
			if("".equals(groupname1)){
				lay_ok.setBackgroundColor(Color.parseColor("#256816"));
			}else{
				lay_ok.setBackgroundColor(Color.parseColor("#3DB516"));
			}
			changeGroupName();
			break;
		case R.id.lay_back_groupname:
			finish();
		default:
			break;
		}
	}

	private void changeGroupName() {
		if (contactgroupService == null) {
			contactgroupService = new ContactGroupService(this);
		}
		contactgroupService.changeGN(groupId, groupname1, new PostRecordResponseListener() {
			
			@Override
			public void onFinish(String succString) {
				if(caseid != null){
					changeCasename(groupname1);
				}
				Intent intent = new Intent();
				intent.putExtra("groupname1", groupname1);
				intent.setAction("name_group");
				setResult(6, intent);
				sendBroadcast(intent);
				finish();
			}
			
			@Override
			public void onFailed(String failString) {
				
			}
		});
	}
	
	public void changeCasename(String name){
		if (caseService == null) {
			caseService = new CaseService(this);
		}
		
		caseService.ChangeCaseDetail(yonghuID, caseid, name, name, "5618bd56bedd324e02543a04", "1", "1", yonghuID, realname, new PostRecordResponseListener() {
			
			@Override
			public void onFinish(String succString) {
				Intent intent3 = new Intent();
				intent3.setAction(Constant.DELETE_HOMELIST);
				sendBroadcast(intent3);
			}
			
			@Override
			public void onFailed(String failString) {
			}
		});
	}

}
